<?php
namespace api\know\controller;

use api\know\model\StudentModel;
use api\know\controller\KnowRestUserController;
use api\know\validate\StudentValidate;

class StudentController extends KnowRestUserController
{
    public function lst()
    {
        $uid = $this->getUserId('uid');

        $chapter_uuid = $this->request->param('chapter_uuid', '');
        $material_uuid = $this->request->param('material_uuid', '');
        $pageNumber = $this->request->param('pageNumber', 1, 'intval');
        $pageSize = $this->request->param('pageSize', 10, 'intval');

        $s_wh[] = ['is_delete', '=', 0];
        $s_wh[] = ['is_enable', '=', 1];

        $total = StudentModel::where($s_wh)->count();
        $rows = StudentModel::where($s_wh)
                                ->field(StudentModel::$QueryField)
                                ->page($pageNumber, $pageSize)
                                ->select();

        $this->success('请求成功!', [ 'total' => $total, 'rows' => $rows]);
    }

    public function add()
    {
        $param = $this->request->param();

        $user = $this->user;
        $param['create_datetime'] = date('Y-m-d H:i:s');
        $param['create_user_id'] = $user['id'];
        $param['create_user'] = $user['user_nickname'];

        try 
		{
            $this->validateFailError($param, 'Student.add');
        } 
		catch (ValidateException $e) 
		{
            $this->error($e->getError());
        }

		$row = StudentModel::create($param);
        if (empty($row))
        {
            $this->error('添加失败 !');
        }
        $this->success('添加成功!', $row);
        
    }

    public function detail()
    {
        $id = $this->request->param('id', '');
        if (empty($id))
        {
            $this->error('id不能为空!');
        }

        $s_wh[] = ['is_delete', '=', 0];
        $s_wh[] = ['is_enable', '=', 1];
        $s_wh[] = ['id', '=', $id];

        $rows = StudentModel::where($s_wh)->field(StudentModel::$QueryField)->find();

        $this->success('请求成功!', $rows);
    }

}
